﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ntc_permit_dept_hr
{
    public partial class Employee : Form
    {
        dbAccess objDB = new dbAccess();

        public Employee()
        {
           
            InitializeComponent();
           
        }

        private void Employee_Load(object sender, EventArgs e)
        {
            
            FillGrid();
            FillEmpID();


            
        }
        public void FillGrid()
        {
            String sql = " select  * from Employee";

            DataTable dt = objDB.SELECT(sql);
            
            dgvEmp.DataSource = dt;
        }

       
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtEmpId.Text=="")
            {
                MessageBox.Show("Enter Employee Id !");
            }
            else if (txtfname.Text=="")
            {
                MessageBox.Show("Enter First Name !");
            }
            else if (txtlname.Text=="")
            {
                MessageBox.Show("Enter Last Name !");
            }
            else if (txtNicNumber.Text=="")
            {
                MessageBox.Show("Enter NIC No !");
            }
            else if (txtNationality.Text=="")
            {
                MessageBox.Show("Enter Nationality !");
            }
            else if (txtAddress.Text=="")
            {
                MessageBox.Show("Enter Address !");
            }
            else if (txtConNo.Text=="")
            {
                MessageBox.Show("Enter Contact No !");
            }
            else if (txtEmail.Text=="")
            {
                MessageBox.Show("Enter Email !");
            }
            else if (txtDes.Text=="")
            {
                MessageBox.Show("Enter Designation !");
            }
            else if (txtExt.Text=="")
            {
                MessageBox.Show("Enter Ext !");
            }
            else if (txtUserName.Text=="")
            {
                MessageBox.Show("Enter User name !");
            }
            else if (txtPword.Text == "")
            {
                MessageBox.Show("Enter Password !");
            }
            else if (!checkNicNo(txtNicNumber.Text))
            {
                 MessageBox.Show("Enter Correct NIC No !");
            }
            else if (!checkTeclNo(txtConNo.Text))
            {
                MessageBox.Show("Enter Correct Contact No !");
            }
            else
            {
                addNewEmployee();
            }
        }

        private void VALUES()
        {
            throw new NotImplementedException();
        }
        void addNewEmployee()
        {
            String gender = "";
            if (rbMale.Checked)
            {
                gender = "M";
            }
            else
            {
                gender = "F";
            }

            String sql = "INSERT INTO Employee values('" + txtEmpId.Text + "','" + txtfname.Text + "','" + txtlname.Text + "','" + gender + "','" + dpBd.Value + "','" + txtNicNumber.Text + "','" + txtNationality.Text + "','" + txtAddress.Text + "','" + txtConNo.Text + "','" + txtEmail.Text + "','" + dpEmpOn.Value + "','" + txtDes.Text + "','" + cmbDep.Text + "','" + txtExt.Text + "','" + cmbULevel.Text + "','" + txtUserName.Text + "','" + txtPword.Text + "','URL')";

            bool isok = objDB.UPDATE(sql);

            if (isok == true)
            {
                MessageBox.Show("Employee detail inserted sucessfully !");
                clearTxt();
                FillGrid();
            }
            else
            {
                MessageBox.Show("Not Valid");
            }


        }

        void clearTxt()
        {
            txtfname.Text = "";
            txtlname.Text = "";
            txtNicNumber.Text = "";
            txtNationality.Text = "";
            txtAddress.Text = "";
            txtConNo.Text = "";
            txtEmail.Text = "";
            txtDes.Text = "";
            txtExt.Text = "";
            txtUserName.Text = "";
            txtPword.Text = "";
            pbPhoto.Image = null;
        }


        public void FillEmpID()
        {
            String sql = " select  EmpId from Employee";

            DataTable dt = objDB.SELECT(sql);
            cmbName.DisplayMember="EmpId";
            cmbName.DataSource = dt;
        }
        void setEmpDetails(string emoid)
        {
            String sql = " select  * from Employee where EmpId='"+emoid+"'";
            DataTable dt = objDB.SELECT(sql);
            txtEmpId.Enabled = false;
            btnAdd.Enabled = false;
            btnUpdate.Enabled = true;
            txtEmpId.Text = dt.Rows[0][0].ToString();
            txtfname.Text = dt.Rows[0][1].ToString();
            txtlname.Text = dt.Rows[0][2].ToString();
            dpBd.Text = dt.Rows[0][4].ToString(); 
            txtNicNumber.Text = dt.Rows[0][5].ToString(); 
            txtNationality.Text = dt.Rows[0][6].ToString(); 
            txtAddress.Text = dt.Rows[0][7].ToString(); 
            txtConNo.Text = dt.Rows[0][8].ToString(); 
            txtEmail.Text = dt.Rows[0][9].ToString();
            dpEmpOn.Text = dt.Rows[0][10].ToString(); 
            txtDes.Text = dt.Rows[0][11].ToString();
            cmbDep.SelectedText = dt.Rows[0][12].ToString(); 
            txtExt.Text = dt.Rows[0][13].ToString();
            cmbULevel.SelectedText = dt.Rows[0][14].ToString(); 
            txtUserName.Text = dt.Rows[0][15].ToString(); 
            txtPword.Text = dt.Rows[0][16].ToString(); 
            pbPhoto.Image = null;
        }
       

        private void cmbName_SelectedIndexChanged(object sender, EventArgs e)
        {
            //
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtEmpId.Text == "")
            {
                MessageBox.Show("Enter Employee Id !");
            }
            else if (txtfname.Text == "")
            {
                MessageBox.Show("Enter First Name !");
            }
            else if (txtlname.Text == "")
            {
                MessageBox.Show("Enter Last Name !");
            }
            else if (txtNicNumber.Text == "")
            {
                MessageBox.Show("Enter NIC No !");
            }
            else if (txtNationality.Text == "")
            {
                MessageBox.Show("Enter Nationality !");
            }
            else if (txtAddress.Text == "")
            {
                MessageBox.Show("Enter Address !");
            }
            else if (txtConNo.Text == "")
            {
                MessageBox.Show("Enter Contact No !");
            }
            else if (txtEmail.Text == "")
            {
                MessageBox.Show("Enter Email !");
            }
            else if (txtDes.Text == "")
            {
                MessageBox.Show("Enter Designation !");
            }
            else if (txtExt.Text == "")
            {
                MessageBox.Show("Enter Ext !");
            }
            else if (txtUserName.Text == "")
            {
                MessageBox.Show("Enter User name !");
            }
            else if (txtPword.Text == "")
            {
                MessageBox.Show("Enter Password !");
            }
            else if (!checkNicNo(txtNicNumber.Text))
            {
                MessageBox.Show("Enter Correct NIC No !");
            }
            else if (!checkTeclNo(txtConNo.Text))
            {
                MessageBox.Show("Enter Correct Contact No !");
            }
            else
            {
                empUpdate();
            }
        }

        void empUpdate()
        {
            btnAdd.Enabled = true;
            String gender = "";
            if (rbMale.Checked)
            {
                gender = "M";
            }
            else
            {
                gender = "F";
            }

            String sql = "update Employee set FirstName ='" + txtfname.Text + "',LastName ='" + txtlname.Text + "',Gender = '" + gender + "',Birthdate = '" + dpBd.Value + "',NIC = '" + txtNicNumber.Text + "',Nationality = '" + txtNationality.Text + "',Address = '" + txtAddress.Text + "',ContactNumber = '" + txtConNo.Text + "',Email = '" + txtEmail.Text + "',EmployedOn = '" + dpEmpOn.Value + "',Designation = '" + txtDes.Text + "',Department = '" + cmbDep.Text + "',Ext = '" + txtExt.Text + "',UserLevel = '" + cmbULevel.Text + "',UserName = '" + txtUserName.Text + "',Password = '" + txtPword.Text + "', PhotoURL = 'URL' where EmpId = '"+txtEmpId.Text+"'";

            bool isok = objDB.UPDATE(sql);

            if (isok == true)
            {
                MessageBox.Show("Employee detail updated sucessfully !");
                btnUpdate.Enabled = true;
                clearTxt();
               
                FillGrid();
            }
            else
            {
                MessageBox.Show("Not Valid");
            }

        }

        Boolean checkNicNo(String nic)
        {
            Boolean st = true;
            for (int i = 0; i < nic.Length; i++)
            {
                char c = nic[i];
                if (i <= 7 && Char.IsLetter(c))
                {
                    st = false;
                }
            }
            if (nic.Length<9 || Char.IsDigit(nic[9]))
            {
                st = false;
            }
            return st;
        }

        Boolean checkTeclNo(String cno)
        {
            Boolean b = true;
            for (int i = 0; i < cno.Length; i++)
            {
                char c = cno[i];
                if (Char.IsLetter(c))
                {
                    b = false;
                }
            }
            return b;
        }
        void searchEmp(int sCat,String sText)
        {
            String sc = "";
            if (sCat==0)
            {
                sc = "EmpId";
            }
            else if (sCat==1)
            {
                sc = "FirstName";
            }
            else if (sCat==2)
            {
                sc = "Address";
            }
            else
            {
                sc = "Department";
            }

            String sql = " select  * from Employee where "+sc+" like '"+sText+"%' order by EmpId";

            DataTable dt = objDB.SELECT(sql);

            dgvEmp.DataSource = dt;
        }

        void deleteEmp(String empId)
        {
            String sql = "delete from employee where EmpId='"+empId+"' ";
            objDB.UPDATE(sql); 
            FillGrid();
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (txtSearch.Text!="" && cmbSearch.SelectedIndex!=-1)
            {
              searchEmp(cmbSearch.SelectedIndex, txtSearch.Text);
            }
            else
            {
                FillGrid();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (dgvEmp.SelectedRows.Count>=1)
            {
                
                 if ((MessageBox.Show("Are you sure you want to delete ? ","Confirme Delete ",MessageBoxButtons.YesNo)==DialogResult.Yes))
                 {
                     string empid = dgvEmp.SelectedRows[0].Cells[0].Value.ToString();
                    deleteEmp(empid);
                 }
            }else
	{
                MessageBox.Show("Select a row to delete !");
	}

         
        }

        private void cmbName_SelectionChangeCommitted(object sender, EventArgs e)
        {
            setEmpDetails(cmbName.Text);
        }

    }
}
